﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AllInOne.Entity;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using RestSharp;
using System.Configuration;
using AllInOne.Models;
using AllInOne.Common;
using System.Collections.ObjectModel;
using AllInOne.ModelDb;

using System.Threading;


namespace AllInOne
{
    public class ServiceWebApi
    {
        public static string BASE_URL = "http://172.59.23.250:9029/api/";
        public static string USER_NAME = "";
        public static void InitProperties(string baseurl)
        {
            //AppSettingsReader reader = new AppSettingsReader();
            //BASE_URL = (string)reader.GetValue("BASE_URL", typeof(string));
            //USER_NAME = (string)reader.GetValue("USER_NAME", typeof(string));

            AppSettingsReader reader = new AppSettingsReader();
            BASE_URL = baseurl;
            USER_NAME = (string)reader.GetValue("USER_NAME", typeof(string));
        }


        public static DrivingLicence GetdrivingLicenseByIdNumber(string idNumber)
        {
            string url = BASE_URL + "/cjg/drivingLicence";
            var client = new RestClient(url);
            var request = new RestRequest();
            request.AddParameter("idNumber", idNumber);
            var response = client.Execute(request);
            JObject json = (JObject)JsonConvert.DeserializeObject(response.Content);
            DrivingLicence drivingLicense = DrivingLicence.ParseObject(json);
            return drivingLicense;
        }

        public static CarInfo GetCarInfo(string carNumber)
        {
            //string url = BASE_URL + "/cjg/carInfo";
            //var client = new RestClient(url);
            //var request = new RestRequest();
            //request.AddParameter("carNumber", carNumber);
            //var response = client.Execute(request);
            //JObject json = (JObject)JsonConvert.DeserializeObject(response.Content);
            //CarInfo carInfo = CarInfo.ParseObject(json);
            //return carInfo;

            return null;
        }

        public static JObject hqzp(string sfzmhm)
        {
            //string url = BASE_URL + "/cjg/hqzp";
            //var client = new RestClient(url);
            //var request = new RestRequest();
            //request.AddParameter("sfzmhm", sfzmhm);
            //var response = client.Execute(request);
            //JObject json = (JObject)JsonConvert.DeserializeObject(response.Content);
            //return json;

            return null;
        }



        public static long UploadTp(string sfzmhm, string xm, string tp, string type)
        {
            //string url = BASE_URL + "/upload/"+type;
            //var client = new RestClient(url);
            //var request = new RestRequest(Method.POST);
            //JObject inputJson = new JObject();
            //inputJson["sfzmhm"] = sfzmhm;
            //inputJson["xm"] = xm;
            //inputJson["tp"] = tp;

            //request.AddParameter("application/json", inputJson.ToString(), ParameterType.RequestBody);

            //var response = client.Execute(request);
            //JObject json = (JObject)JsonConvert.DeserializeObject(response.Content);
            //long sfzId =  (long)json["id"];
            //return sfzId;
            long sfzId = 1233;
            return sfzId;
        }

        public static JObject bzhz(BusinessInfo businessInfo, DrivingLicence drivingLicense)
        {
            //SimpleConsole.WriteInfo("开始提交", new ServiceApi());
            //string url = BASE_URL + "/cjg/bzhz";
            //var client = new RestClient(url);
            //var request = new RestRequest();
            //request.AddParameter("ywlx", (int)businessInfo.businessType);
            //request.AddParameter("sfzmhm", drivingLicense.sfzmhm);
            //request.AddParameter("xm", drivingLicense.xm);
            //request.AddParameter("dabh", drivingLicense.dabh);
            //request.AddParameter("xb", drivingLicense.xb);
            //request.AddParameter("csrq", drivingLicense.csrq);
            //request.AddParameter("yjdz", drivingLicense.lxzsxxdz);
            //request.AddParameter("lxzsxzqh", drivingLicense.lxzsxzqh);
            //request.AddParameter("gddh", drivingLicense.lxdh);
            //request.AddParameter("dzxx", drivingLicense.dzyx);
            //request.AddParameter("sjhm", drivingLicense.sjhm);
            //request.AddParameter("xsjhm",businessInfo.sjhm==null?"":businessInfo.sjhm);
            //request.AddParameter("yzbm", drivingLicense.lxzsyzbm);
            //request.AddParameter("qmId", businessInfo.qmId);
            //request.AddParameter("xczpId", businessInfo.xczpId);
            //request.AddParameter("sfzId", businessInfo.sfzId);
            //request.AddParameter("yhdh", USER_NAME);
            //request.AddParameter("zpId", businessInfo.zpId);
            //var response = client.Execute(request);
            //SimpleConsole.WriteInfo(response.Content, new ServiceApi());
            //JObject json = (JObject)JsonConvert.DeserializeObject(response.Content);
            //return json;

            return null;
        }

        public static JObject cxlsxx(string lsh)
        {
            //string url = BASE_URL + "/cjg/cxlsxx";
            //var client = new RestClient(url);
            //var request = new RestRequest();
            //request.AddParameter("lsh", lsh);
            //var response = client.Execute(request);
            //JObject json = (JObject)JsonConvert.DeserializeObject(response.Content);
            //return json;

            return null;
        }

        public static JObject lnmj(BusinessInfo businessInfo, CarInfo carInfo, string glbm)
        {
            //string url = BASE_URL + "/cjg/lnmj";
            //var client = new RestClient(url);
            //var request = new RestRequest();
            //request.AddParameter("glbm", glbm);
            //SimpleConsole.WriteInfo("hpzl"+carInfo.hpzl,new ServiceApi());
            //request.AddParameter("hpzl", carInfo.hpzl);
            //request.AddParameter("fzjg", carInfo.fzjg);
            //request.AddParameter("hphm", carInfo.hphm);
            //request.AddParameter("xh", carInfo.xh);
            //request.AddParameter("zzrq", carInfo.bxzzrq);
            //request.AddParameter("sfzId", businessInfo.sfzId);
            //request.AddParameter("qmId", businessInfo.qmId);
            //request.AddParameter("xm", carInfo.syr);
            //request.AddParameter("sfzmhm", carInfo.sfzmhm);
            //request.AddParameter("sjhm", carInfo.sjhm);
            //request.AddParameter("gddh", carInfo.lxdh);
            //request.AddParameter("yjdz", carInfo.zsxxdz);

            //var response = client.Execute(request);
            //JObject json = (JObject)JsonConvert.DeserializeObject(response.Content);
            //return json;

            return null;
        }

        public static List<T_Dictionary> GetDicByType(string DicType)
        {
            try
            {
                string url = BASE_URL + "Common/GetDicByDicType";
                var client = new RestClient(url);
                var request = new RestRequest();
                request.AddParameter("DicType", DicType);
                var response = client.Execute(request);

                if (!string.IsNullOrEmpty(response.Content))
                {
                    List<T_Dictionary> lsit = JsonConvert.DeserializeObject<List<T_Dictionary>>(response.Content);
                    return lsit;
                }
                else
                {
                    return null;
                }
            }
            catch (Exception ex)
            {
                SimpleConsole.WriteError("请求接口出错" + ex.ToString(), null);
                return null;
            }
        }

        public static bool Login(out string msg, object obj)
        {
            msg = "";
            try
            {
                string url = BASE_URL + "User/Login";
                var client = new RestClient(url);
                var request = new RestRequest(Method.POST);
                string body = Newtonsoft.Json.JsonConvert.SerializeObject(obj);
                request.AddJsonBody(body);

                var response = client.Execute(request);

                if (!string.IsNullOrEmpty(response.Content))
                {
                    BackInfo backInfo = JsonConvert.DeserializeObject<BackInfo>(response.Content);
                    if (backInfo.code == 200)
                    {
                        msg = "成功";
                        return true;
                    }
                    else
                    {
                        msg = "登录失败";
                        return false;
                    }
                }
                else
                {
                    msg = "登录失败，请检查网络";
                    return false;
                }
            }
            catch (Exception ex)
            {
                msg = "登录失败，请检查网络";
                SimpleConsole.WriteError("Login 登录失败" + ex.ToString(), null);
                return false;
            }
        }

        /// <summary>
        /// 新增入厂--注册信息
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static bool AddRegisterInfo(T_RegisterInfo obj)
        {
            try
            {
                string url = BASE_URL + "SSMachine/AddRegisterInfo";
                var client = new RestClient(url);
                var request = new RestRequest(Method.POST);
                string body = Newtonsoft.Json.JsonConvert.SerializeObject(obj);
                request.AddJsonBody(body);
                var response = client.Execute(request);
                if (!string.IsNullOrEmpty(response.Content))
                {
                    BackInfo backInfo = JsonConvert.DeserializeObject<BackInfo>(response.Content);
                    if (backInfo.code == 200)
                    {
                        return true;
                    }
                    else
                    {
                        SimpleConsole.WriteError("AddRegisterInfo 新增数据失败:" + backInfo.message, null);
                        return false;
                    }
                }
                else
                {
                    SimpleConsole.WriteError("AddRegisterInfo 新增数据失败", null);
                    return false;
                }
            }
            catch (Exception ex)
            {
                SimpleConsole.WriteError("请求接口出错" + ex.ToString(), null);
                return false;
            }
        }

        /// <summary>
        /// 根据车牌和备案号 检查硫酸--是否可以拉
        /// </summary>
        /// <param name="plate">车牌号</param>
        /// <param name="strYNumber">备案号</param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public static bool CheckSulphuricAcid(string plate, string strYNumber, out string msg)
        {
            msg = "";
            try
            {
                string url = BASE_URL + "SSMachine/CheckSulphuricAcid";
                var client = new RestClient(url);
                var request = new RestRequest(Method.POST);
                //request.AddHeader("Accept", "*/*");//‘可省略
                request.AddParameter("plate", plate);
                request.AddParameter("strYNumber", strYNumber);
                var response = client.Execute(request);
                if (!string.IsNullOrEmpty(response.Content))
                {
                    BackInfo backInfo = JsonConvert.DeserializeObject<BackInfo>(response.Content);
                    if (backInfo.code == 200)
                    {
                        return true;
                    }
                    else
                    {
                        //将错误信息组合起来传到前台
                        string outMsg = "车号：" + plate + " 备案号：" + strYNumber + " 验证失败信息为：" + backInfo.message;
                        msg = outMsg;
                        SimpleConsole.WriteError("CheckSulphuricAcid 检查此辆车是否有运输硫酸权限--无权:" + backInfo.message, null);
                        return false;
                    }
                }
                else
                {
                    SimpleConsole.WriteError("CheckSulphuricAcid 检查此辆车是否有运输硫酸权限--失败", null);
                    return false;
                }
            }
            catch (Exception ex)
            {
                SimpleConsole.WriteError("CheckSulphuricAcid 检查此辆车是否有运输硫酸权限--出错" + ex.ToString(), null);
                return false;
            }
        }


        /// <summary>
        /// 根据车牌和备案号 检查硫酸--是否可以拉
        /// </summary>
        /// <param name="plate">车牌号</param> 
        /// <param name="strGCompany">购买单位（即收货单位）</param>
        /// <param name="strYCompany">运输单位</param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public static bool CheckSulphuricAcidByInfo(string plate, string strGCompany, string strYCompany, out string msg)
        {
            msg = "";
            try
            {
                string url = BASE_URL + "SSMachine/CheckSulphuricAcidByInfo";
                var client = new RestClient(url);
                var request = new RestRequest(Method.POST);
                //request.AddHeader("Accept", "*/*");//‘可省略
                request.AddParameter("plate", plate);
                request.AddParameter("strGCompany", strGCompany);
                request.AddParameter("strYCompany", strYCompany);
                var response = client.Execute(request);
                if (!string.IsNullOrEmpty(response.Content))
                {
                    BackInfo backInfo = JsonConvert.DeserializeObject<BackInfo>(response.Content);

                    if (backInfo.code == 200)
                    {
                        msg = backInfo.data.ToString();
                        return true;
                    }
                    else
                    {
                        msg = backInfo.message;
                        //将错误信息组合起来传到前台
                        return false;
                    }
                }
                else
                {
                    SimpleConsole.WriteError("CheckSulphuricAcidByInfo 检查此辆车是否有运输硫酸权限--失败", null);
                    return false;
                }
            }
            catch (Exception ex)
            {
                SimpleConsole.WriteError("CheckSulphuricAcidByInfo 检查此辆车是否有运输硫酸权限--出错" + ex.ToString(), null);
                return false;
            }
        }


        /// <summary>
        /// 查询所有注册信息
        /// </summary>
        /// <param name="pageIndex">页号</param>
        /// <param name="pageSize">页大小</param>
        /// <param name="totalCount">总条数</param>
        /// <param name="iCardNum">卡号</param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public static BackInfo<ObservableCollection<T_RegisterInfo>> QueryRegisterInfoByPage(int pageIndex, int pageSize, out int totalCount, string iCardNum, out string msg)
        {
            msg = "";
            totalCount = 0;
            BackInfo<ObservableCollection<T_RegisterInfo>> backInfo;
            try
            {
                string url = BASE_URL + "SSMachine/QueryRegisterInfoByPage";
                var client = new RestClient(url);
                var request = new RestRequest(Method.POST);
                request.AddParameter("pageIndex", pageIndex);
                request.AddParameter("pageSize", pageSize);
                request.AddParameter("iCardNum", iCardNum);
                var response = client.Execute(request);
                if (!string.IsNullOrEmpty(response.Content))
                {
                    backInfo = JsonConvert.DeserializeObject<BackInfo<ObservableCollection<T_RegisterInfo>>>(response.Content);

                    //var registerInfo = JsonConvert.DeserializeObject<RegisterInfo>(result.data.ToString());
                    //return registerInfo;

                    return backInfo;
                }
                else
                {
                    SimpleConsole.WriteError("QueryRegisterInfoByPage 查询注册信息--失败", null);
                    return null;
                }
            }
            catch (Exception ex)
            {
                SimpleConsole.WriteError("QueryRegisterInfoByPage 查询注册信息--出错" + ex.ToString(), null);
                return null;
            }
        }

        /// <summary>
        /// 根据流水号查询
        /// </summary>
        /// <param name="lsh">流水号</param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public static JObject QuerySerialNumber(string lsh)
        {
            JObject result = new JObject();
            try
            {
                string url = BASE_URL + "SSMachine/QuerySerialNumber";
                var client = new RestClient(url);
                var request = new RestRequest(Method.POST);
                request.AddParameter("lsh", lsh);

                var response = client.Execute(request);

                if (!string.IsNullOrEmpty(response.Content))
                {
                    BackInfo backInfo = (BackInfo)JsonConvert.DeserializeObject(response.Content);
                    if (backInfo.code == 200)
                    {
                        return (JObject)backInfo.data;
                    }
                    else
                    {
                        SimpleConsole.WriteError("QuerySerialNumber 检查流水信息--为空:" + backInfo.message, null);
                        return result;
                    }
                }
                else
                {
                    SimpleConsole.WriteError("QuerySerialNumber 检查流水信息--失败", null);
                    return result;
                }
            }
            catch (Exception ex)
            {
                SimpleConsole.WriteError("QuerySerialNumber 检查流水信息--出错" + ex.ToString(), null);
                return result;
            }
        }

        /// <summary>
        /// 更新制卡信息的卡号
        /// </summary>
        /// <param name="guid">注删记录唯一识别号</param>
        /// <param name="card">卡号</param>
        /// <returns></returns>
        public static bool UpdataRegisterInfo(T_RegisterInfo t_RegisterInfo, out string msg)
        {
            JObject result = new JObject();
            try
            {
                string url = BASE_URL + "SSMachine/UpdataRegisterInfo";
                var client = new RestClient(url);
                var request = new RestRequest(Method.POST);
                string body = Newtonsoft.Json.JsonConvert.SerializeObject(t_RegisterInfo);
                request.AddJsonBody(body);
                var response = client.Execute(request);
                if (!string.IsNullOrEmpty(response.Content))
                {
                    BackInfo backInfo = JsonConvert.DeserializeObject<BackInfo>(response.Content);
                    //BackInfo backInfo = (BackInfo)JsonConvert.DeserializeObject(response.Content);
                    msg = backInfo.message;
                    if (backInfo.code == 200)
                    {
                        return true;
                    }
                    else
                    {
                        SimpleConsole.WriteError("UpdataRegisterInfo 更新数据失败:" + backInfo.message, null);
                        return false;
                    }
                }
                else
                {
                    SimpleConsole.WriteError("UpdataRegisterInfo 更新数据失败。", null);
                    msg = "更新数据失败";
                    return false;
                }
            }
            catch (Exception ex)
            {
                SimpleConsole.WriteError("UpdataRegisterInfo 更新数据--失败" + ex.ToString(), null);
                msg = "更新数据失败";
                return false;
            }
        }


        #region 查询基础信息 //基础信息同步到本地
        ///// <summary>
        ///// 货物、发货单位、收货单位
        ///// GetProductList、
        ///// </summary>
        ///// <param name="MethodName">调用接口的方法名字--</param>
        ///// <returns></returns>
        //public static List<ReceiverList> GetBaseData(string MethodName)
        //{
        //    try
        //    {
        //        string url = BASE_URL + "Common/" + MethodName;

        //        var client = new RestClient(url);
        //        var request = new RestRequest();
        //        var response = client.Execute(request);

        //        if (!string.IsNullOrEmpty(response.Content))
        //        {
        //            BackInfo backInfo = JsonConvert.DeserializeObject<BackInfo>(response.Content);
        //            if (backInfo.code == 200)
        //            {
        //                List<ReceiverList> ReList = ConvertToList.ConvertToReceiverList(backInfo.data);
        //                if (ReList != null)
        //                {
        //                    return ReList;
        //                }
        //            }
        //        }
        //        return null;
        //    }
        //    catch (Exception ex)
        //    {
        //        SimpleConsole.WriteError("请求接口出错" + ex.ToString(), null);
        //        return null;
        //    }
        //}

        /// <summary>
        /// 货物
        /// GetProductList、
        /// </summary>
        /// <returns></returns>
        public static List<ProductList> GetProductList()
        {
            try
            {
                string url = BASE_URL + "Common/GetProductList";

                var client = new RestClient(url);
                var request = new RestRequest();
                var response = client.Execute(request);

                if (!string.IsNullOrEmpty(response.Content))
                {
                    BackInfo backInfo = JsonConvert.DeserializeObject<BackInfo>(response.Content);
                    if (backInfo.code == 200)
                    {
                        List<ProductList> ReList = ConvertToList.ConvertToProductList(backInfo.data);
                        if (ReList != null)
                        {
                            return ReList;
                        }
                    }
                }
                return null;
            }
            catch (Exception ex)
            {
                SimpleConsole.WriteError("请求接口出错" + ex.ToString(), null);
                return null;
            }
        }

        /// <summary>
        /// 发货单位--company表不再要了
        /// 日期：2023年4月14日
        /// GetProductList
        /// </summary>
        /// <returns></returns>
        public static List<Company> GetCompany()
        {
            List<Company> listCompany = new List<Company>();
            try
            {
                string url = BASE_URL + "Company/GetList";

                var client = new RestClient(url);
                var request = new RestRequest(Method.POST);
                var response = client.Execute(request);
                if (!string.IsNullOrEmpty(response.Content))
                {
                    ReturnModel<ObservableCollection<Company>> ret = JsonConvert.DeserializeObject<ReturnModel<ObservableCollection<Company>>>(response.Content);
                    if (ret.Code == 200 && ret.Data != null)
                    {
                        foreach (Company item in ret.Data)
                        {
                            listCompany.Add(item);
                        }
                    }
                }
                return listCompany;
            }
            catch (Exception ex)
            {
                SimpleConsole.WriteError("请求接口出错" + ex.ToString(), null);
                return listCompany;
            }
        }

        /// <summary>
        /// 发货单位
        /// GetProductList、
        /// </summary>
        /// <returns></returns>
        public static List<SenderList> GetSenderList()
        {
            try
            {
                string url = BASE_URL + "Common/GetSenderList";

                var client = new RestClient(url);
                var request = new RestRequest();
                var response = client.Execute(request);

                if (!string.IsNullOrEmpty(response.Content))
                {
                    BackInfo backInfo = JsonConvert.DeserializeObject<BackInfo>(response.Content);
                    if (backInfo.code == 200)
                    {
                        List<SenderList> ReList = ConvertToList.ConvertToSenderList(backInfo.data);
                        if (ReList != null)
                        {
                            return ReList;
                        }
                    }
                }
                return null;
            }
            catch (Exception ex)
            {
                SimpleConsole.WriteError("请求接口出错" + ex.ToString(), null);
                return null;
            }
        }

        /// <summary>
        /// 货物
        /// </summary>
        /// <param name="MethodName">调用接口的方法名字--</param>
        /// <returns></returns>
        public static List<ReceiverList> GetReceiverList()
        {
            try
            {
                string url = BASE_URL + "Common/GetReceiverList";

                var client = new RestClient(url);
                var request = new RestRequest();
                var response = client.Execute(request);

                if (!string.IsNullOrEmpty(response.Content))
                {
                    BackInfo backInfo = JsonConvert.DeserializeObject<BackInfo>(response.Content);
                    if (backInfo.code == 200)
                    {
                        List<ReceiverList> ReList = ConvertToList.ConvertToReceiverList(backInfo.data);
                        if (ReList != null)
                        {
                            return ReList;
                        }
                    }
                }
                return null;
            }
            catch (Exception ex)
            {
                SimpleConsole.WriteError("请求接口出错" + ex.ToString(), null);
                return null;
            }
        }

        /// <summary>
        /// 查询运输单位
        /// </summary>
        /// <returns></returns>
        public static List<TransporterList> GetTransporterList()
        {
            try
            {
                string url = BASE_URL + "Common/GetTransporterList";

                var client = new RestClient(url);
                var request = new RestRequest();
                var response = client.Execute(request);

                if (!string.IsNullOrEmpty(response.Content))
                {
                    BackInfo backInfo = JsonConvert.DeserializeObject<BackInfo>(response.Content);
                    if (backInfo.code == 200)
                    {
                        List<TransporterList> ReList = ConvertToList.ConvertToTransporterList(backInfo.data);
                        if (ReList != null)
                        {
                            return ReList;
                        }
                    }
                }
                return null;
            }
            catch (Exception ex)
            {
                SimpleConsole.WriteError("请求接口出错" + ex.ToString(), null);
                return null;
            }
        }

        /// <summary>
        /// 获取自定义基础数据
        /// </summary>
        /// <returns></returns>
        public static List<CustomBaseData> GetCustomBaseData(string CustomerType)
        {
            try
            {
                string url = BASE_URL + "Common/QueryCustomBaseDataByParameter";
                var client = new RestClient(url);
                var request = new RestRequest(Method.POST);
                request.AddParameter("BaseDataType", CustomerType);//查询自助定义类型数据
                var response = client.Execute(request);

                if (!string.IsNullOrEmpty(response.Content))
                {
                    BackInfo backInfo = JsonConvert.DeserializeObject<BackInfo>(response.Content);
                    if (backInfo.code == 200)
                    {
                        List<CustomBaseData> ReList = ConvertToList.ConvertToCustomBaseData(backInfo.data);
                        if (ReList != null)
                        {
                            return ReList;
                        }
                    }
                }
                return null;
            }
            catch (Exception ex)
            {
                SimpleConsole.WriteError("请求接口出错" + ex.ToString(), null);
                return null;
            }
        }

        #endregion


        
        /// <summary>
        /// 查询数库里的最后一条记录的发卡号
        /// </summary>
        /// <param name="plate">车牌号</param>
        /// <param name="strYNumber">备案号</param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public static bool QueryRegisterInfoOrderNumber(string plate, string strYNumber, out string msg)
        {
            msg = "";
            try
            {
                string url = BASE_URL + "SSMachine/QueryRegisterInfoOrderNumber";
                var client = new RestClient(url);
                var request = new RestRequest(Method.POST);
                request.AddParameter("plate", plate);
                request.AddParameter("strYNumber", strYNumber);

                var response = client.Execute(request);

                if (!string.IsNullOrEmpty(response.Content))
                {
                    BackInfo backInfo = (BackInfo)JsonConvert.DeserializeObject(response.Content);
                    if (backInfo.code == 200)
                    {
                        return true;
                    }
                    else
                    {
                        //将错误信息组合起来传到前台
                        string data = backInfo.data.ToString();
                        string outMsg = "车号：" + plate + " 备案号：" + strYNumber + " 验证失败信息为：";
                        var array = data.Split(',');
                        for (int i = 0, j = 1; i < array.Length; i++)
                        {
                            if (array[i] != "")
                            {
                                switch (array[i])
                                {
                                    case "1":
                                        outMsg += " 此备案号不存在;";
                                        break;
                                    case "2":
                                        outMsg += " 此备案号信息已经超期;";
                                        break;
                                    case "3":
                                        outMsg += " 此车辆未登记在备案信息中;";
                                        break;
                                    case "4":
                                        outMsg += " 此车辆今日已经拉过不可再拉;";
                                        break;
                                    default:
                                        outMsg += " 验证未通过;";
                                        break;
                                }
                            }
                        }
                        msg = outMsg;
                        SimpleConsole.WriteError("CheckSulphuricAcid 检查此辆车是否有运输硫酸权限--无权:" + backInfo.message, null);
                        return false;
                    }
                }
                else
                {
                    SimpleConsole.WriteError("CheckSulphuricAcid 检查此辆车是否有运输硫酸权限--失败", null);
                    return false;
                }
            }
            catch (Exception ex)
            {
                SimpleConsole.WriteError("CheckSulphuricAcid 检查此辆车是否有运输硫酸权限--出错" + ex.ToString(), null);
                return false;
            }
        }

        /// <summary>
        /// 根据卡号获取卡的注册信息
        /// </summary>
        /// <param name="number"></param>
        /// <returns></returns>
        public static T_RegisterInfo GetRegisterInfoByCardNumber(string number)
        {
            IRestResponse response = null;
            try
            {
                //string url = BASE_URL + "SSMachine/QueryRegisterInfoOrderNumber";
                //var client = new RestClient(url);
                //var request = new RestRequest(Method.POST);
                var client = new RestClient($"{BASE_URL}SSMachine/QueryRegisterInfoByCardNum?CardNum={number}")
                {
                    Timeout = 2000
                };
                var request = new RestRequest(Method.POST);
                response = client.Execute(request);

                BackInfo backInfo = JsonConvert.DeserializeObject<BackInfo>(response.Content);
                if (backInfo != null && backInfo.code == 200)
                {
                    if (backInfo.data != null)
                    {
                        var registerInfo = JsonConvert.DeserializeObject<T_RegisterInfo>(backInfo.data.ToString());
                        return registerInfo;
                    }
                }
            }
            catch (Exception ex)
            {
                SimpleConsole.WriteError("请求接口出错" + ex.ToString(), null);
            }
            return null;
        }

        /// <summary>
        /// 还卡
        /// </summary>
        /// <returns></returns>
        public static bool BackICard(T_RegisterInfo t_RegisterInfo, out string msg)
        {
            JObject result = new JObject();
            try
            {
                string url = BASE_URL + "SSMachine/BackICard";
                var client = new RestClient(url);
                var request = new RestRequest(Method.POST);
                string body = Newtonsoft.Json.JsonConvert.SerializeObject(t_RegisterInfo);
                request.AddJsonBody(body);
                var response = client.Execute(request);
                if (!string.IsNullOrEmpty(response.Content))
                {
                    BackInfo backInfo = JsonConvert.DeserializeObject<BackInfo>(response.Content);
                    msg = backInfo.message;
                    if (backInfo.code == 200)
                    {
                        return true;
                    }
                    else
                    {
                        SimpleConsole.WriteError("BackICard 还卡更新数据失败:" + backInfo.message, null);
                        return false;
                    }
                }
                else
                {
                    SimpleConsole.WriteError("BackICard 还卡更新数据失败。", null);
                    msg = "还卡更新数据失败";
                    return false;
                }
            }
            catch (Exception ex)
            {
                SimpleConsole.WriteError("BackICard 还卡更新数据--失败" + ex.ToString(), null);
                msg = "还卡更新数据失败";
                return false;
            }
        }


        #region 手动发卡所需接口
        /// <summary>
        /// 根据IC卡号查找此卡是否归还
        /// </summary>
        /// <param name="CardNum">卡号</param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public static bool QueryBackByCardNum(string CardNum, out string msg)
        {
            msg = "";
            try
            {
                string url = BASE_URL + "SSMachine/QueryBackByCardNum";
                var client = new RestClient(url);
                var request = new RestRequest(Method.POST);
                request.AddParameter("CardNum", CardNum);
                var response = client.Execute(request);
                if (!string.IsNullOrEmpty(response.Content))
                {
                    BackInfo backInfo = JsonConvert.DeserializeObject<BackInfo>(response.Content);
                    msg = backInfo.message;
                    if (backInfo.code == 200 && (int)backInfo.data == 1)
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
                else
                {
                    SimpleConsole.WriteError("QueryBackByCardNum 验证此卡的注册信息--失败", null);
                    return false;
                }
            }
            catch (Exception ex)
            {
                SimpleConsole.WriteError("QueryBackByCardNum 验证此卡的注册信息--出错" + ex.ToString(), null);
                return false;
            }
        }

        #endregion


        #region 数据展示所需接口
        /// <summary>
        /// 门卫信息展示
        /// </summary>
        /// <param name="date">日期</param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public static List<T_RegisterInfo> QueryToGuardOffice(string date, out string msg)
        {
            msg = "";
            try
            {
                string url = BASE_URL + "SSMachine/QueryToGuardOffice";
                var client = new RestClient(url);
                var request = new RestRequest(Method.POST);
                request.AddParameter("date", date);
                var response = client.Execute(request);
                if (!string.IsNullOrEmpty(response.Content))
                {
                    //
                    BackInfo backInfo = JsonConvert.DeserializeObject<BackInfo>(response.Content);
                    msg = backInfo.message;
                    if (backInfo.code == 200)
                    {
                        return JsonConvert.DeserializeObject<List<T_RegisterInfo>>(backInfo.data.ToString());
                        //var re = backInfo.data;
                        //return null;


                    }
                    else
                    {
                        return null;
                    }
                }
                else
                {
                    SimpleConsole.WriteError("QueryToGuardOffice 门卫信息展示--失败", null);
                    return null;
                }
            }
            catch (Exception ex)
            {
                SimpleConsole.WriteError("QueryToGuardOffice 门卫信息展示--出错" + ex.ToString(), null);
                return null;
            }
        }

        #endregion

        #region 验证车牌列表
        /// <summary>
        /// 验证车牌列表
        /// </summary>
        /// <param name="Date">日期</param>
        /// <param name="GoodsName">货物名字</param>
        /// <returns></returns>
        public static List<VerifyPlateList> QueryVerifyPlateList(string Date, string GoodsName, out string msg)
        {
            msg = "";
            try
            {
                string url = BASE_URL + "Common/QueryVerifyPlateList";
                var client = new RestClient(url);
                var request = new RestRequest(Method.POST);
                request.AddParameter("Date", Date);
                request.AddParameter("GoodsName", GoodsName);
                var response = client.Execute(request);
                if (!string.IsNullOrEmpty(response.Content))
                {
                    ReturnModel<List<VerifyPlateList>> ret = JsonConvert.DeserializeObject<ReturnModel<List<VerifyPlateList>>>(response.Content);
                    msg = ret.Message;
                    if (ret.Code == 200 && ret.Data != null)
                        return ret.Data;
                    else
                    {
                        return null;
                    }
                }
                else
                {
                    msg = "请检查连接服务器是否正常,获取数据失败";
                    SimpleConsole.WriteError("QueryVerifyPlateList 请检查连接服务器是否正常,获取数据--失败", null);
                    return null;
                }
            }
            catch (Exception ex)
            {
                SimpleConsole.WriteError("QueryVerifyPlateList 请检查连接服务器是否正常,获取数据--失败" + ex.ToString(), null);
                return null;
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="Date"></param>
        /// <param name=""></param>
        /// <returns></returns>
        public static List<VerifyPlateList> VerifyPlate(string Date, string GoodsName, out string msg)
        {
            msg = "";
            try
            {
                string url = BASE_URL + "Common/QueryVerifyPlateList";
                var client = new RestClient(url);
                var request = new RestRequest(Method.POST);
                request.AddParameter("Date", Date);
                request.AddParameter("GoodsName", GoodsName);
                var response = client.Execute(request);
                if (!string.IsNullOrEmpty(response.Content))
                {
                    ReturnModel<List<VerifyPlateList>> ret = JsonConvert.DeserializeObject<ReturnModel<List<VerifyPlateList>>>(response.Content);
                    msg = ret.Message;
                    if (ret.Code == 200 && ret.Data != null)
                        return ret.Data;
                    else
                    {
                        return null;
                    }
                }
                else
                {
                    msg = "请检查连接服务器是否正常,获取数据失败";
                    SimpleConsole.WriteError("QueryVerifyPlateList 请检查连接服务器是否正常,获取数据--失败", null);
                    return null;
                }
            }
            catch (Exception ex)
            {
                SimpleConsole.WriteError("QueryVerifyPlateList 请检查连接服务器是否正常,获取数据--失败" + ex.ToString(), null);
                return null;
            }
        }


        /// <summary>
        /// 验证车牌列表
        /// </summary>
        /// <param name="Date">日期</param>
        /// <param name="GoodsName">货物名字</param>
        /// <param name="Plate">货物名字</param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public static bool VerifyPlate(string Date, string GoodsName, string Plate, out string msg)
        {
            msg = "";
            try
            {
                string url = BASE_URL + "Common/VerifyPlate";
                var client = new RestClient(url);
                var request = new RestRequest(Method.POST);
                request.AddParameter("Date", Date);
                request.AddParameter("GoodsName", GoodsName);
                request.AddParameter("Plate", Plate);
                var response = client.Execute(request);
                if (!string.IsNullOrEmpty(response.Content))
                {
                    BackInfo backInfo = JsonConvert.DeserializeObject<BackInfo>(response.Content);
                    if (backInfo.code == 200)
                    {
                        return true;
                    }
                    else
                    {
                        //将错误信息组合起来传到前台
                        msg = backInfo.message;
                        return false;
                    }
                }
                else
                {
                    SimpleConsole.WriteError("VerifyPlate 检查此辆车是否有运输权限--失败" + Date + GoodsName + Plate, null);
                    msg = "请检查网是否正常连接";
                    return false;
                }
            }
            catch (Exception ex)
            {
                SimpleConsole.WriteError("VerifyPlate 检查此辆车是否有运输权限--出错" + Date + GoodsName + Plate + ex.ToString(), null);
                msg = "请检查网是否正常连接";
                return false;
            }
        }


        #endregion

        #region 查询注册信息并显示
        /// <summary>
        /// 查询注册信息
        /// </summary>
        /// <param name="Date">日期</param>
        /// <param name="GoodsName">货物名字</param>
        /// <returns></returns>
        public static List<T_RegisterInfo> QueryRegByParameter(Dictionary<string,string> dic, out string msg)
        {
            msg = "";
            try
            {
                string url = BASE_URL + "SSMachine/QueryRegByParameter";
                var client = new RestClient(url);
                var request = new RestRequest(Method.POST);
                Dictionary<string, string> ParameterDic = new Dictionary<string, string>();
                QueryParameter queryParameter = new QueryParameter();
                queryParameter.pageIndex = 0;
                queryParameter.ParameterDic = new Dictionary<string, string>();
                //参数
                queryParameter.ParameterDic = dic;
                request.AddJsonBody(queryParameter);
                var response = client.Execute(request);
                if (!string.IsNullOrEmpty(response.Content))
                {
                    ReturnModel<List<T_RegisterInfo>> ret = JsonConvert.DeserializeObject<ReturnModel<List<T_RegisterInfo>>>(response.Content);
                    msg = ret.Message;
                    if (ret.Code == 200 && ret.Data != null)
                        return ret.Data;
                    else
                    {
                        return null;
                    }
                }
                else
                {
                    msg = "请检查连接服务器是否正常,获取数据失败";
                    SimpleConsole.WriteError("QueryRegByParameter 请检查连接服务器是否正常,获取数据--失败", null);
                    return null;
                }
            }
            catch (Exception ex)
            {
                SimpleConsole.WriteError("QueryRegByParameter 请检查连接服务器是否正常,获取数据--失败" + ex.ToString(), null);
                return null;
            }
        }

        #endregion


        #region 连接服务器
        /// <summary>
        /// 系统统一的登录模型
        /// </summary>
        /// <param name="message_show">信息提示方法</param>
        /// <param name="start_update">启动更新方法</param>
        /// <param name="thread_finish">线程结束后的复原方法</param>
        /// <param name="userName">用户名</param>
        /// <param name="password">密码</param>
        /// <param name="remember">是否记住登录密码</param>
        /// <param name="clientType">客户端登录类型</param>
        /// <returns></returns>
        public static bool CheckConnectServer(Action<string> message_show)
        {
            message_show.Invoke("正在检查...");
            Thread.Sleep(200);

            try
            {
                string url = BASE_URL + "Common/GetAllBlackList";
                var client = new RestClient(url);
                var request = new RestRequest();
                var response = client.Execute(request);
                if (!string.IsNullOrEmpty(response.Content))
                {
                    BackInfo backInfo = JsonConvert.DeserializeObject<BackInfo>(response.Content);
                    if (backInfo.code == 200 || backInfo.code == 201)//有返回，则认为是连通的
                    {
                        List<BlackList> ResultList = ConvertToList.ConvertToBlackList(backInfo.data);
                        //
                        return true;
                    }
                }
                //服务器连接不对
                return false;
            }
            catch (Exception ex)
            {
                SimpleConsole.WriteError("请求接口出错" + ex.ToString(), null);
                return false;
            }
        }
        #endregion


        #region 信息查询
        /// <summary>
        /// 功能：验证车牌列表
        /// 日期：2023年3月30日
        /// </summary>
        /// <param name="Date">日期</param>
        /// <param name="CardType">类型</param>
        /// <param name="Plate">车牌</param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public static List<VerifyPlateList> GetTheSameDayGoodsByPlate(string Date, string CardType, string Plate, out string msg)
        {
            msg = "";
            try
            {
                string url = BASE_URL + "Common/GetTheSameDayGoodsByPlate";
                var client = new RestClient(url);
                var request = new RestRequest(Method.POST);
                request.AddParameter("Date", Date);
                request.AddParameter("CardType", CardType);
                request.AddParameter("Plate", Plate);
                var response = client.Execute(request);
                if (!string.IsNullOrEmpty(response.Content))
                {
                    BackInfo backInfo = JsonConvert.DeserializeObject<BackInfo>(response.Content);
                    if (backInfo.code == 200)
                    {
                        return JsonConvert.DeserializeObject<List<VerifyPlateList>>(backInfo.data.ToString());
                    }
                    else
                    {
                        //将错误信息组合起来传到前台
                        msg = backInfo.message;
                        return null;
                    }
                }
                else
                {
                    SimpleConsole.WriteError($"GetTheSameDayGoodsByPlate方法，请检查日期为：{Date} 辆车为：{Plate} 是否有运输权限--失败" , null);
                    msg = "请检查网是否正常连接";
                    return null;
                }
            }
            catch (Exception ex)
            {
                SimpleConsole.WriteError($"GetTheSameDayGoodsByPlate方法，请检查日期为：{Date} 辆车为：{Plate} 是否有运输权限--出错" + ex.ToString(), null);
                msg = "请检查网是否正常连接";
                return null;
            }
        }
        #endregion
    }
}
